<?php

namespace app\admin\controller\property;

use app\common\controller\Backend;
use think\Db;

/**
 * 物品出库-选择列表
 *
 * @icon fa fa-circle-o
 */
class Repertorysss extends Backend
{

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Repertory;
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with([
                    'vindicate' => function ($query) {
                        $query->withField('id,name,specification');
                    }
                ])
                ->where($where)
                ->order('id', 'desc')
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }


    /**
     * 详情
     */
    public function detail($ids)
    {


        //$row = $this->model->get(['id' => $ids]);
        $row = Db::table('fa_repertory')
            ->alias('a')
            ->field('a.id,b.name')
            ->join('fa_vindicate b','a.vindicate_id=b.id','left')
            ->where(['a.id'=>['in',$ids]])
            ->order('a.id','desc')
            ->select();

        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 领取
     */
    public function edits(){
        if ($this->request->isPost()) {
            $row = $this->request->post("row/a", [], 'trim');
            if ($row) {

                //开启事务
                Db::startTrans();

                $arr = [];
                foreach ($row['count'] as $k=>$v){
                    if (!is_numeric($v)){
                        $this->error(__('数量只能为数字类型'));
                    }

                    $user_arr = Db::table('fa_repertory')
                        ->where('id',$k)
                        ->find();

                    if (empty($user_arr)){
                        $this->error(__('库存数据为空'));
                    }

                    if (($user_arr['count']-$v) < 0){
                        $this->error(__('库存数量不足'));
                    }

                    $arr[$k]['repertory_id'] = $k;
                    $arr[$k]['admin_id'] = $this->auth->id;
                    $arr[$k]['admin_name'] = $this->auth->nickname;
                    $arr[$k]['msg'] = $row['msg'][$k];
                    $arr[$k]['count'] = $v;
                    $arr[$k]['type'] = 2;
                    $arr[$k]['create_time'] = time();
                    $arr[$k]['vindicate_id'] = $user_arr['vindicate_id'];

                    $update = Db::table('fa_repertory')
                        ->where('id',$k)
                        ->setDec('count',$v);

                    if ($update === false){
                        //事务回滚
                        Db::rollback();
                        $this->error(__('失败'));
                    }
                }

                $arr = array_merge($arr);

                $insert = Db::table('fa_removal')
                    ->insertAll($arr);

                if ($insert > 0){
                    //事务提交
                    Db::commit();
                    $this->success('成功');
                }else{
                    //事务回滚
                    Db::rollback();
                    $this->error(__('失败'));
                }


            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
    }


}
